<?php

/**
 * restaurant actions.
 *
 * @package    halalmenu_dev
 * @subpackage restaurant
 * @author     Saidi AHAMADA
 * @version    SVN: $Id: actions.class.php 23810 2012-03-04 11:07:44Z Saidi.AHAMADA $
 */

class restaurantActions extends sfActions
{

  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('default', 'module');
  }
  
  public function executeShowAmbiance(sfWebRequest $request)
  {
    $this->pager = new sfDoctrinePager('HalalmenuRestaurant',sfConfig::get('app_pagination'));
    $this->pager->setQuery($this->retrieveRestaurantsByAmbiance($request->getParameter('id')));
    $this->pager->setPage($request->getParameter('page',1));
    $this->pager->init();
    
    $this->nb = count($this->pager);

    $this->restaurant_cuisine = Doctrine_Core::getTable('HalalmenuRestaurantCuisine')->getAllCuisines();
    $this->restaurants_stat = Doctrine::getTable('HalalmenuRestaurantCommentsByUser')->calculRestaurantStatByAmbiance($request->getParameter('id'));
    $this->restaurant_list = Doctrine_Core::getTable('HalalmenuRestaurant')->getRestaurantList();
    $this->restaurantInfo = array();
    
    $this->idService = $request->getParameter('id');
    $this->service = Doctrine_Core::getTable('HalalmenuAtmosphere')->find($request->getParameter('id'));
            
    foreach($this->restaurants_stat as $restaurant){
       $this->restaurantInfo[$restaurant['idrestaurant']] = array(
                                                  "nbreviews" => $restaurant['nbreviews'],
                                                  "average"  => $restaurant['average'],
                                                  "cuisine" => array()
                                                ); 
       
    foreach($this->restaurant_cuisine as $cuisine){      
           if ($restaurant['idrestaurant'] == $cuisine['idrestaurant']){
                $this->mycuisine = "test";
                $this->restaurantInfo[$restaurant['idrestaurant']]["cuisine"][] = $cuisine['HalalmenuCuisine']['cuisine'];
       
           }
       }
   }
   
   $this->cityFilterCuisine = $this->retrieveRestaurantAmbianceCities($request->getParameter('id'));
   $this->departementFilterCuisine = $this->retrieveRestaurantAmbianceDepartements($request->getParameter('id'));
    

   $this->setTemplate('ShowFilterService');
   
  }
  
  public function executeShowFilterService(sfWebRequest $request)
  {
    $this->pager = new sfDoctrinePager('HalalmenuRestaurant',sfConfig::get('app_pagination'));
    $this->pager->setQuery($this->retrieveRestaurantsByService($request->getParameter('id')));
    $this->pager->setPage($request->getParameter('page',1));
    $this->pager->init();
    
    $this->nb = count($this->pager);

    $this->restaurant_cuisine = Doctrine_Core::getTable('HalalmenuRestaurantCuisine')->getAllCuisines();
    $this->restaurants_stat = Doctrine::getTable('HalalmenuRestaurantCommentsByUser')->calculRestaurantStatByService($request->getParameter('id'));
    $this->restaurant_list = Doctrine_Core::getTable('HalalmenuRestaurant')->getRestaurantList();
    $this->restaurantInfo = array();
    
    $this->idService = $request->getParameter('id');
    $this->service = Doctrine_Core::getTable('HalalmenuService')->find($request->getParameter('id'));
            
    foreach($this->restaurants_stat as $restaurant){
       $this->restaurantInfo[$restaurant['idrestaurant']] = array(
                                                  "nbreviews" => $restaurant['nbreviews'],
                                                  "average"  => $restaurant['average'],
                                                  "cuisine" => array()
                                                ); 
       
    foreach($this->restaurant_cuisine as $cuisine){      
           if ($restaurant['idrestaurant'] == $cuisine['idrestaurant']){
                $this->mycuisine = "test";
                $this->restaurantInfo[$restaurant['idrestaurant']]["cuisine"][] = $cuisine['HalalmenuCuisine']['cuisine'];
       
           }
       }
   }
   
   $this->cityFilterCuisine = $this->retrieveRestaurantServiceCities($request->getParameter('id'));
   $this->departementFilterCuisine = $this->retrieveRestaurantServiceDepartements($request->getParameter('id'));
   
//   if (!$this->restaurants)
//    $this->forward404Unless($this->restaurant);
  }

  public function executeShow(sfWebRequest $request)
  {

    $this->pager = new sfDoctrinePager('HalalmenuRestaurant',sfConfig::get('app_pagination'));
    $this->pager->setQuery($this->getCuisineRestaurants($request->getParameter('id')));
    $this->pager->setPage($request->getParameter('page',1));
    $this->pager->init();
    $this->restaurantInfo = array();
    foreach($this->pager->getResults() as $restaurant )
    {
        $listCuisines = Doctrine_Core::getTable('HalalmenuRestaurantCuisine')->getListRestaurantCuisine($restaurant->getHalalmenuRestaurant()->getId());
        foreach($listCuisines as $cuisine){
            $this->restaurantInfo[$restaurant->getIdRestaurant()]["cuisine"][] = $cuisine['HalalmenuCuisine'];
        }
    }
    //echo '<pre>';
    //print_r($this->restaurantInfo);
    //exit;
    $this->nb = count($this->pager);

    $this->restaurant_cuisine = Doctrine_Core::getTable('HalalmenuRestaurantCuisine')->getAllCuisines();
    $this->restaurants_stat = Doctrine::getTable('HalalmenuRestaurantCommentsByUser')->calculStatByRestaurant($request->getParameter('id'));
    $this->restaurant_list = Doctrine_Core::getTable('HalalmenuRestaurant')->getRestaurantList();
    
    
    $this->idCuisine = $request->getParameter('id');
    $this->cuisine = Doctrine_Core::getTable('HalalmenuCuisine')->find($request->getParameter('id'));
            
//    foreach($this->restaurant_list as $restaurant){
//       $this->restaurantInfo[$restaurant->getId()] = array(
//                                                  "nbreviews" => 0, //$restaurant['nbreviews'],
//                                                  "average"  =>0, // $restaurant['average'],
//                                                  "cuisine" => array()
//                                                ); 
//       $this->mycuisine = 0;
//    foreach($this->restaurant_cuisine as $cuisine){  
//        
//           if ($restaurant->getId() == $cuisine['idrestaurant']){
//               
//                $this->mycuisine = $this->mycuisine + 1;
//                $this->restaurantInfo[$restaurant->getId()]["cuisine"][] = $cuisine['HalalmenuCuisine']['cuisine'];
//           }
//       }   
//   }
   //echo $this->mycuisine;
   
   $this->cityFilterCuisine = $this->retrieveRestaurantCities($request->getParameter('id'));
   $this->departementFilterCuisine = $this->retrieveRestaurantDepartement($request->getParameter('id'));

//   if (!$this->restaurants)
//    $this->forward404Unless($this->restaurant);
   $this->region = array(
                         "Paris" => "75", 
                         "Seine-et-Marne" => "77", 
                         "Yvelines" => "78",
                         "Essonne" => "91",
                         "Haut-de-Seine" => "92",
                         "Seine-Saint-Denis"=>"93",
                         "Val-de-Marne"=>"94",
                         "Val-d'Oise"=>"95",
                         "Val-d-Oise"=> "95"
                        ); 
  }
  //HERE
  
  public function executeCuisineVilleShow(sfWebRequest $request)
  { 
    $this->pager = new sfDoctrinePager('HalalmenuRestaurant',sfConfig::get('app_pagination'));
    $this->pager->setQuery($this->retrieveRestaurantByDepartementVille($request->getParameter('id'),$request->getParameter('ville')));
    $this->pager->setPage($request->getParameter('page',1));
    $this->pager->init();
    
    $this->nb = count($this->pager);
    $this->restaurantInfo = array();
    foreach($this->pager->getResults() as $restaurant )
    {
        $listCuisines = Doctrine_Core::getTable('HalalmenuRestaurantCuisine')->getListRestaurantCuisine($restaurant->getId());
        foreach($listCuisines as $cuisine){
            $this->restaurantInfo[$restaurant->getId()]["cuisine"][] = $cuisine['HalalmenuCuisine'];
        }
    }
    $this->key = 'app_departement_'.$request->getParameter('id');

    $this->restaurant_cuisine = Doctrine_Core::getTable('HalalmenuRestaurantCuisine')->getAllCuisines();
    $this->restaurants_stat = Doctrine::getTable('HalalmenuRestaurantCommentsByUser')->calculStatByRestaurantByDept($request->getParameter('id'));
    $this->restaurant_list = Doctrine_Core::getTable('HalalmenuRestaurant')->getRestaurantList();
   
 
   $this->departement = $request->getParameter('id');
   $this->cityFilterCuisine = $this->getNbRestaurantByCuisine($request->getParameter('id'));
   $this->departementFilterCity = $this->getNbRestaurantByCity($request->getParameter('id'));

   $this->tabDept = array(
                        '75' => array('id' => 75, 'departement' => 'paris'),
                        '77' => array('id' => 77, 'departement' => 'seine-et-marne'),
                        '78' => array('id' => 78, 'departement' => 'yvelines'),
                        '91' => array('id' => 91, 'departement' => 'essonne'),
                        '92' => array('id' => 92, 'departement' => 'haut-de-seine'),
                        '93' => array('id' => 93, 'departement' => 'saint-denis'),
                        '94' => array('id' => 94, 'departement' => 'val-de-marne'),
                        '95' => array('id' => 95, 'departement' => 'val-d-oise')
                        );
  }
  public function executeFilterShow(sfWebRequest $request)
  {
    
    $numDept = $request->getParameter('id');
    $idCuisine = $request->getParameter('cuisine');
    $this->cuisinelibelle = Doctrine::getTable('HalalmenuCuisine')->find($idCuisine);
    $this->pager = new sfDoctrinePager('HalalmenuRestaurant',sfConfig::get('app_pagination'));
    $this->pager->setQuery($this->retrieveRestaurantByDepartementCuisine($numDept,$idCuisine));
    $this->pager->setPage($request->getParameter('page',1));
    $this->pager->init();
    
    $this->nb = count($this->pager);
    $this->restaurantInfo = array();
    foreach($this->pager->getResults() as $restaurant )
    {
        $listCuisines = Doctrine_Core::getTable('HalalmenuRestaurantCuisine')->getListRestaurantCuisine($restaurant->getHalalmenuRestaurant()->getId());
        foreach($listCuisines as $cuisine){
            $this->restaurantInfo[$restaurant->getHalalmenuRestaurant()->getId()]["cuisine"][] = $cuisine['HalalmenuCuisine'];
        }
    }
    $this->key = 'app_departement_'.$request->getParameter('id');

    $this->restaurant_cuisine = Doctrine_Core::getTable('HalalmenuRestaurantCuisine')->getAllCuisines();
    $this->restaurants_stat = Doctrine::getTable('HalalmenuRestaurantCommentsByUser')->calculStatByRestaurantByDept($request->getParameter('id'));
    $this->restaurant_list = Doctrine_Core::getTable('HalalmenuRestaurant')->getRestaurantList();
    
    $this->departement = $request->getParameter('id');
    
  }
  
  public function executeDepartementShow(sfWebRequest $request)
  {
          
    $this->pager = new sfDoctrinePager('HalalmenuRestaurant',sfConfig::get('app_pagination'));
    //echo $request->getParameter('cuisine'); exit;
    $this->pager->setQuery($this->retrieveRestaurantByDepartement($request->getParameter('id')));
    $this->pager->setPage($request->getParameter('page',1));
    $this->pager->init();
    
    $this->nb = count($this->pager);
    $this->restaurantInfo = array();
    foreach($this->pager->getResults() as $restaurant )
    {
        $listCuisines = Doctrine_Core::getTable('HalalmenuRestaurantCuisine')->getListRestaurantCuisine($restaurant->getId());
        foreach($listCuisines as $cuisine){
            $this->restaurantInfo[$restaurant->getId()]["cuisine"][] = $cuisine['HalalmenuCuisine'];
        }
    }
    $this->key = 'app_departement_'.$request->getParameter('id');

    $this->restaurant_cuisine = Doctrine_Core::getTable('HalalmenuRestaurantCuisine')->getAllCuisines();
    $this->restaurants_stat = Doctrine::getTable('HalalmenuRestaurantCommentsByUser')->calculStatByRestaurantByDept($request->getParameter('id'));
    $this->restaurant_list = Doctrine_Core::getTable('HalalmenuRestaurant')->getRestaurantList();
   
    
    
    //$this->cuisine = Doctrine_Core::getTable('HalalmenuCuisine')->find($request->getParameter('id'));
  
//    foreach($this->restaurants_stat as $restaurant)
//    {
//       $this->restaurantInfo[$restaurant['idrestaurant']] = array(
//                                                  "nbreviews" => NULL ,
//                                                  "average"  => NULL     ,
//                                                  "cuisine" => array(NULL)
//                                                ); 
//
//        foreach($this->restaurant_cuisine as $cuisine)
//        {      
//            if ($restaurant['idrestaurant'] == $cuisine['idrestaurant'])
//            {                
//                $this->restaurantInfo[$restaurant['idrestaurant']]["cuisine"][] = NULL; //$cuisine['HalalmenuCuisine']['cuisine'];
//            }
//        }
//    }
 
   $this->departement = $request->getParameter('id');
   $this->cityFilterCuisine = $this->getNbRestaurantByCuisine($request->getParameter('id'));
   $this->departementFilterCity = $this->getNbRestaurantByCity($request->getParameter('id'));

   //$this->cuisine_list    = Doctrine_Core::getTable('HalalmenuCuisine')->getCuisinesList();
//   if (!$this->restaurants)
//    $this->forward404Unless($this->restaurant);
   
   $this->tabDept = array(
                        '75' => array('id' => 75, 'departement' => 'paris'),
                        '77' => array('id' => 77, 'departement' => 'seine-et-marne'),
                        '78' => array('id' => 78, 'departement' => 'yvelines'),
                        '91' => array('id' => 91, 'departement' => 'essonne'),
                        '92' => array('id' => 92, 'departement' => 'haut-de-seine'),
                        '93' => array('id' => 93, 'departement' => 'saint-denis'),
                        '94' => array('id' => 94, 'departement' => 'val-de-marne'),
                        '95' => array('id' => 95, 'departement' => 'val-d-oise')
                        );
  }
  
  public function executeDepartementCuisineShow(sfWebRequest $request)
  {
          
    $this->pager = new sfDoctrinePager('HalalmenuRestaurant',sfConfig::get('app_pagination'));
    $this->pager->setQuery($this->retrieveRestaurantByDepartement($request->getParameter('id')));
    $this->pager->setPage($request->getParameter('page',1));
    $this->pager->init();
    
    $this->nb = count($this->pager);
    $this->restaurantInfo = array();
    foreach($this->pager->getResults() as $restaurant )
    {
        $listCuisines = Doctrine_Core::getTable('HalalmenuRestaurantCuisine')->getListRestaurantCuisine($restaurant->getId());
        foreach($listCuisines as $cuisine){
            $this->restaurantInfo[$restaurant->getId()]["cuisine"][] = $cuisine['HalalmenuCuisine'];
        }
    }
    $this->key = 'app_departement_'.$request->getParameter('id');

    $this->restaurant_cuisine = Doctrine_Core::getTable('HalalmenuRestaurantCuisine')->getAllCuisines();
    $this->restaurants_stat = Doctrine::getTable('HalalmenuRestaurantCommentsByUser')->calculStatByRestaurantByDept($request->getParameter('id'));
    $this->restaurant_list = Doctrine_Core::getTable('HalalmenuRestaurant')->getRestaurantList();
   
    
    
    //$this->cuisine = Doctrine_Core::getTable('HalalmenuCuisine')->find($request->getParameter('id'));
  
//    foreach($this->restaurants_stat as $restaurant)
//    {
//       $this->restaurantInfo[$restaurant['idrestaurant']] = array(
//                                                  "nbreviews" => NULL ,
//                                                  "average"  => NULL     ,
//                                                  "cuisine" => array(NULL)
//                                                ); 
//
//        foreach($this->restaurant_cuisine as $cuisine)
//        {      
//            if ($restaurant['idrestaurant'] == $cuisine['idrestaurant'])
//            {                
//                $this->restaurantInfo[$restaurant['idrestaurant']]["cuisine"][] = NULL; //$cuisine['HalalmenuCuisine']['cuisine'];
//            }
//        }
//    }
 
   $this->departement = $request->getParameter('id');
   $this->cityFilterCuisine = $this->getNbRestaurantByCuisine($request->getParameter('id'));
   $this->departementFilterCity = $this->getNbRestaurantByCity($request->getParameter('id'));

   //$this->cuisine_list    = Doctrine_Core::getTable('HalalmenuCuisine')->getCuisinesList();
//   if (!$this->restaurants)
//    $this->forward404Unless($this->restaurant);
  }
  
  public function executeDepartementVilleShow(sfWebRequest $request)
  {
    $this->pager = new sfDoctrinePager('HalalmenuRestaurant',sfConfig::get('app_pagination'));
    $this->pager->setQuery($this->retrieveRestaurantByDepartementVille($request->getParameter('id'),$request->getParameter('ville')));
    $this->pager->setPage($request->getParameter('page',1));
    $this->pager->init();
    
    $this->nb = count($this->pager);
    $this->restaurantInfo = array();
    foreach($this->pager->getResults() as $restaurant )
    {
        $listCuisines = Doctrine_Core::getTable('HalalmenuRestaurantCuisine')->getListRestaurantCuisine($restaurant->getId());
        foreach($listCuisines as $cuisine){
            $this->restaurantInfo[$restaurant->getId()]["cuisine"][] = $cuisine['HalalmenuCuisine'];
        }
    }
    $this->key = 'app_departement_'.$request->getParameter('id');

    $this->restaurant_cuisine = Doctrine_Core::getTable('HalalmenuRestaurantCuisine')->getAllCuisines();
    $this->restaurants_stat = Doctrine::getTable('HalalmenuRestaurantCommentsByUser')->calculStatByRestaurantByDept($request->getParameter('id'));
    $this->restaurant_list = Doctrine_Core::getTable('HalalmenuRestaurant')->getRestaurantList();
   
 
   $this->departement = $request->getParameter('id');
   $this->cityFilterCuisine = $this->getNbRestaurantByCuisine($request->getParameter('id'));
   $this->departementFilterCity = $this->getNbRestaurantByCity($request->getParameter('id'));

   $this->tabDept = array(
                        '75' => array('id' => 75, 'departement' => 'paris'),
                        '77' => array('id' => 77, 'departement' => 'seine-et-marne'),
                        '78' => array('id' => 78, 'departement' => 'yvelines'),
                        '91' => array('id' => 91, 'departement' => 'essonne'),
                        '92' => array('id' => 92, 'departement' => 'haut-de-seine'),
                        '93' => array('id' => 93, 'departement' => 'saint-denis'),
                        '94' => array('id' => 94, 'departement' => 'val-de-marne'),
                        '95' => array('id' => 95, 'departement' => 'val-d-oise')
                        );
  }
  
  public function executeRestaurantInfo(sfWebRequest $request)
  {
      $this->getUser()->setAttribute('prix',"");
      $this->getUser()->setAttribute('ambiance',"");
      $this->getUser()->setAttribute('service',"");
      $this->getUser()->setAttribute('nourriture',"");
      $this->restaurantInfo     =  $this->retrieveOneRestaurant($request->getParameter('id'));
      $this->restaurantInfo     =  $this->restaurantInfo[0];
      
      $this->getUser()->setAttribute('idrestaurant',$request->getParameter('id'));

      $this->restaurantCuisines =  $this->retrieveListCuisine($request->getParameter('id'));
      
      $this->restaurantReviews  =  $this->retrieveReviewRestaurant($request->getParameter('id')); 
      $this->restaurantRate      =  ($this->getRateRestaurant($request->getParameter('id')) != NULL) ? $this->getRateRestaurant($request->getParameter('id')) :NULL ;
      $this->restaurantAmbiance =  $this->retrieveRestaurantListAmbiances($request->getParameter('id'));
      $this->restaurantServices =  $this->retrieveRestaurantListServices($request->getParameter('id'));
      $this->restaurantPayments = $this->retrieveListRestaurantPayments($request->getParameter('id'));
      
      $chaineCuisine = "";
      foreach($this->restaurantCuisines as $cuisine)
          $chaineCuisine = $chaineCuisine.ucFirst($cuisine).', '; 
      $chaineCuisine[strlen($chaineCuisine)-2] = '.';
      
      $chaineService = "";
      foreach($this->restaurantServices as $service)
         $chaineService = $chaineService.ucFirst($service).', ';
      $chaineService[strlen($chaineService)-2] = '.';
     
        
      $chaineAmbiance = "";
      foreach($this->restaurantAmbiance as $ambiance)
         $chaineAmbiance = $chaineAmbiance.ucFirst($ambiance).', ';
      $chaineAmbiance[strlen($chaineAmbiance)-2] = '.';
      
      $chainePayments = "";
      foreach($this->restaurantPayments as $payment)
         $chainePayments = $chainePayments.ucFirst($payment).', ';
      $chainePayments[strlen($chainePayments)-2] = '.';
      
      $this->liste_service  =  (($chaineService == NULL)? '' : $chaineService);
      $this->liste_ambiance =  (($chaineAmbiance == NULL)? '' : $chaineAmbiance);
      $this->liste_cuisine  =  (($chaineCuisine == NULL)? '' : $chaineCuisine); 
      
      
      $this->liste_payments =  $chainePayments;
      $this->moyenneMenuRestaurant = $this->getMenuAverage($request->getParameter('id'));
      
      $this->Departement = array(
                        '75' => array('id' => 75, 'departement' => 'paris'),
                        '77' => array('id' => 77, 'departement' => 'seine-et-marne'),
                        '78' => array('id' => 78, 'departement' => 'yvelines'),
                        '91' => array('id' => 91, 'departement' => 'essonne'),
                        '92' => array('id' => 92, 'departement' => 'haut-de-seine'),
                        '93' => array('id' => 93, 'departement' => 'saint-denis'),
                        '94' => array('id' => 94, 'departement' => 'val-de-marne'),
                        '95' => array('id' => 95, 'departement' => 'val-d-oise')
                        );
  }
  
  public function executeMenu(sfWebRequest $request)
  {
      
      $this->restaurantInfo     =  $this->retrieveOneRestaurant($request->getParameter('id'));
      $this->restaurantInfo     =  $this->restaurantInfo[0];
      $this->restaurantCuisines =  $this->retrieveListCuisine($request->getParameter('id'));
      $this->restaurantReviews  =  $this->retrieveReviewRestaurant($request->getParameter('id')); 
      $this->retaurantRate      =  $this->getRateRestaurant($request->getParameter('id'));
      
      $this->restaurantMeal     =  $this->retrieveRestaurantMeal($request->getParameter('id')); 
      $this->restaurantCategory =  $this->retrieveRestaurantCategoryInMeal($request->getParameter('id'));
      $this->moyenneMenuRestaurant = $this->getMenuAverage($request->getParameter('id'));
      
      $this->Departement = array(
                '75' => array('id' => 75, 'departement' => 'paris'),
                '77' => array('id' => 77, 'departement' => 'seine-et-marne'),
                '78' => array('id' => 78, 'departement' => 'yvelines'),
                '91' => array('id' => 91, 'departement' => 'essonne'),
                '92' => array('id' => 92, 'departement' => 'haut-de-seine'),
                '93' => array('id' => 93, 'departement' => 'saint-denis'),
                '94' => array('id' => 94, 'departement' => 'val-de-marne'),
                '95' => array('id' => 95, 'departement' => 'val-d-oise')
                );
      
   
  }
  
  public function executePrice(sfWebRequest $request)
  {
      $tab[1] = sfConfig::get('app_price_average_1');
      $tab[2] = sfConfig::get('app_price_average_2');
      $tab[3] = sfConfig::get('app_price_average_3');
      $tab[4] = sfConfig::get('app_price_average_4');
      
      $min = $tab[$request->getParameter('id')]['min'];
      $max = $tab[$request->getParameter('id')]['max'];
      $this->path = "Entre ".$min." et ".$max." €";
      $this->pager = new sfDoctrinePager('HalalmenuRestaurant',sfConfig::get('app_pagination'));
      $this->pager->setQuery($this->retrieveRestaurantByAverageMenu($min,$max));
      $this->pager->setPage($request->getParameter('page',1));
      $this->pager->init();

      $this->nb = count($this->pager);

      $this->restaurant_cuisine = Doctrine_Core::getTable('HalalmenuRestaurantCuisine')->getAllCuisines();
      $this->restaurants_stat = Doctrine::getTable('HalalmenuRestaurantCommentsByUser')->calculRestaurantStatByPrice($min,$max);
      $this->restaurant_list = Doctrine_Core::getTable('HalalmenuRestaurant')->getRestaurantList();
    $this->restaurantInfo = array();
    
    $this->idService = $request->getParameter('id');
    $this->service = Doctrine_Core::getTable('HalalmenuService')->find($request->getParameter('id'));
            
    foreach($this->restaurants_stat as $restaurant){
       $this->restaurantInfo[$restaurant['idrestaurant']] = array(
                                                  "nbreviews" => $restaurant['nbreviews'],
                                                  "average"  => $restaurant['average'],
                                                  "cuisine" => array()
                                                ); 
       
    foreach($this->restaurant_cuisine as $cuisine){      
           if ($restaurant['idrestaurant'] == $cuisine['idrestaurant']){
                $this->mycuisine = "test";
                $this->restaurantInfo[$restaurant['idrestaurant']]["cuisine"][] = $cuisine['HalalmenuCuisine']['cuisine'];
       
           }
       }
   }
   
   $this->cityFilterCuisine = $this->retrieveRestaurantServiceCities($request->getParameter('id'));
   $this->departementFilterCuisine = $this->retrieveRestaurantServiceDepartements($request->getParameter('id'));
      

  }
  
  public function retrieveRestaurantsByAmbiance($idAmbiance)
  {
      return Doctrine_Core::getTable('HalalmenuRestaurant')->retrieveRestaurantsByAmbiance($idAmbiance); 
  }
  
  public function retrieveRestaurantByAverageMenu($min, $max)
  {
      return Doctrine_Core::getTable('HalalmenuMeal')->retrieveRestaurantByAverageMenu($min, $max);
  }
  
  public function getMenuAverage($idRestaurant)
  {
      return Doctrine_Core::getTable('HalalmenuMeal')->getMenuAverage($idRestaurant);
  }
  
  public function retrieveRestaurantsByService($idService)
  {
      return Doctrine_Core::getTable('HalalmenuRestaurant')->retrieveRestaurantsByService($idService); 
  }
  
  public function getCuisineRestaurants($idCuisine) 
  {
      return Doctrine_Core::getTable('HalalmenuRestaurant')->getCuisineRestaurants($idCuisine);
  }
  
  public function getNbRestaurantByCuisine($idRestaurant)
  {    
      return Doctrine_Core::getTable('HalalmenuCity')->retrieveNbCuisineByDept($idRestaurant);
  }
  
  public function getNbRestaurantByCity($idRestaurant)
  {
      return Doctrine_Core::getTable('HalalmenuCity')->retrieveNbCityByDept($idRestaurant);
  }
  
  public function retrieveRestaurantByDepartement($idDepartement)
  {   
      return Doctrine_Core::getTable('HalalmenuRestaurant')->retrieveRestaurantByDepartement($idDepartement);
  }
  
  public function retrieveRestaurantByDepartementVille($idDepartement,$idVille)
  {
      return Doctrine_Core::getTable('HalalmenuRestaurant')->retrieveRestaurantByDepartementVille($idDepartement,$idVille);
  }
  public function retrieveCuisineByVille($idCuisine,$idVille)
  {
   //   return Docytine_Core::getTable('HalalmenuRestau')
  }
  public function retrieveRestaurantByDepartementCuisine($idDepartement,$cuisine)
  {   
      return Doctrine_Core::getTable('HalalmenuRestaurant')->retrieveRestaurantByDepartementCuisine($idDepartement,$cuisine);
  }
  public function retrieveRestaurantByVille($idVille)
  {
      return Doctrine_Core::getTable('HalalmenuRestaurant')->retrieveRestaurantByVille($idVille);
  }
  public function getRateRestaurant($idRestaurant)
  {
    $restaurantReviews = $this->retrieveReviewRestaurant($idRestaurant); 
    if ($restaurantReviews == NULL)
    {
        $sum = 0;
        $average = 0;
        foreach($restaurantReviews as $i => $restaurant)
        {
            $sum = ($restaurant->getRatePrice() + $restaurant->getRateAmbiance() + $restaurant->getRateService() + $restaurant->getRateNourriture());
            $average = ($sum/4);
            $tabAverage[$i] = $average;
        }
        $averageRestaurantRate = (array_sum($tabAverage) / ($i + 1));
        return $averageRestaurantRate;
    }
     return NULL;
   
  }
  
  public function retrieveListRestaurantPayments($idRestaurant)
  {
      return Doctrine_Core::getTable('HalalmenuRestaurantPayment')->retrieveListRestaurantPayments($idRestaurant);
  }
  
  public function retrieveRestaurantMeal($idRestaurant)
  {
      return Doctrine_Core::getTable('HalalmenuMeal')->retrieveRestaurantMeal($idRestaurant);
  }
  
  public function retrieveRestaurantCategoryInMeal($idRestaurant)
  {
      return Doctrine_Core::getTable('HalalmenuMeal')->retrieveRestaurantCategoryInMeal($idRestaurant);
  }
  
  public function retrieveRestaurantListAmbiances($idRestaurant)
  {
      return Doctrine_Core::getTable('HalalmenuRestaurantAtmosphere')->retrieveRestaurantListAmbiances($idRestaurant);
  }
  
  public function retrieveRestaurantListServices($idRestaurant)
  {
      return Doctrine_Core::getTable('HalalmenuRestaurantService')->retrieveRestaurantListServices($idRestaurant);
  }
  
  public function retrieveReviewRestaurant($idRestauant)
  {
      return Doctrine_Core::getTable('HalalmenuRestaurantCommentsByUser')->retrieveReviewRestaurant($idRestauant);
  }
  
  public function retrieveListCuisine($idRestaurant)
  {
      return Doctrine_Core::getTable('HalalmenuRestaurantCuisine')->retrieveListRestaurantCuisine($idRestaurant);
  }
  
  public function retrieveRestaurantCities($idCuisine)
  {
      return Doctrine_Core::getTable('HalalmenuCity')->retrieveRestaurantCities($idCuisine);
  }
  
  public function retrieveRestaurantServiceCities($idService)
  {
      return Doctrine_Core::getTable('HalalmenuCity')->retrieveRestaurantServiceCities($idService);
  }
  
  public function retrieveRestaurantServiceDepartements($idService)
  {
      $tabDepartement = array();
      $listdepartement = Doctrine_Core::getTable('HalalmenuCity')->retrieveRestaurantServiceDepartements($idService);
      $nomDepartement = array("75" => "Paris",
                              "77" => "Seine et Marne",
                              "78" =>"Les Yvelines", 
                              "91" => "Essonne", 
                              "92" => "Haut-de-Seine", 
                              "93"=>"Seine-Saint-Denis", 
                              "94"=>"Val-de-Marne",
                              "95"=>"Val-d'Oise" );
      foreach($listdepartement as $departement)
      {
          $tabDepartement[] = array(
                                    "nomDepartement" => $nomDepartement[$departement['departement']],
                                    "nbrestaurant"             => $departement['nbrestaurant']
                                    );
      }
      return $tabDepartement;
  }
  
  public function retrieveRestaurantDepartement($idCuisine)
  {
      $tabDepartement = array();
      $listdepartement = Doctrine_Core::getTable('HalalmenuCity')->retrieveRestaurantDepartement($idCuisine);
      $nomDepartement = array("75" => "Paris",
                              "77" => "Seine et Marne",
                              "78" =>"Les Yvelines", 
                              "91" => "Essonne", 
                              "92" => "Haut-de-Seine", 
                              "93"=>"Seine-Saint-Denis", 
                              "94"=>"Val-de-Marne",
                              "95"=>"Val-d-Oise" );
      foreach($listdepartement as $departement)
      {
          $tabDepartement[] = array(
                                    "nomDepartement" => $nomDepartement[$departement['departement']],
                                    "nbrestaurant"             => $departement['nbrestaurant']
                                    );
      }
      return $tabDepartement;
  }
  
  public function retrieveOneRestaurant($idRestaurant)
  {
     return Doctrine_Core::getTable('HalalmenuRestaurant')->retrieveOneRestaurant($idRestaurant);     
  }
  
  public function retrieveRestaurantAmbianceCities($idAmbiance)
  {
      return Doctrine_Core::getTable('HalalmenuCity')->retrieveRestaurantAmbianceCities($idAmbiance);
  }
  
  public function retrieveRestaurantAmbianceDepartements($idAmbiance)
  {
      $tabDepartement = array();
      $listdepartement = Doctrine_Core::getTable('HalalmenuCity')->retrieveRestaurantAmbianceDepartements($idAmbiance);
      $nomDepartement = array("75" => "Paris",
                              "77" => "Seine et Marne",
                              "78" =>"Les Yvelines", 
                              "91" => "Essonne", 
                              "92" => "Haut-de-Seine", 
                              "93"=>"Seine-Saint-Denis", 
                              "94"=>"Val-de-Marne",
                              "95"=>"Val-d'Oise" );
      foreach($listdepartement as $departement)
      {
          $tabDepartement[] = array(
                                    "nomDepartement" => $nomDepartement[$departement['departement']],
                                    "nbrestaurant"             => $departement['nbrestaurant']
                                    );
      }
      return $tabDepartement;
  }
  
  public function executePDF(sfWebRequest $request)
  {
  
      $this->restaurantInfo     =  $this->retrieveOneRestaurant($request->getParameter('id'));
      $this->restaurantInfo     =  $this->restaurantInfo[0];
      $this->restaurantCuisines =  $this->retrieveListCuisine($request->getParameter('id'));
      $this->restaurantReviews  =  $this->retrieveReviewRestaurant($request->getParameter('id')); 
      $this->retaurantRate      =  $this->getRateRestaurant($request->getParameter('id'));
      
      $this->restaurantMeal     =  $this->retrieveRestaurantMeal($request->getParameter('id')); 
      $this->restaurantCategory =  $this->retrieveRestaurantCategoryInMeal($request->getParameter('id'));
      $this->moyenneMenuRestaurant = $this->getMenuAverage($request->getParameter('id'));
      
      $config = sfTCPDFPluginConfigHandler::loadConfig();
 
     $pdf = new sfTCPDF();  // pdf object
 
     $pdf->SetCreator(PDF_CREATOR);   // set document information
     $pdf->SetAuthor('Halalmenu');
     $pdf->SetTitle('Menu et Carte Halalmenu');
     $pdf->SetSubject('menu');
     $pdf->SetKeywords('menu, halalmenu, halal, menu, guide');

    // set default header data
     $pdf->setHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, $this->restaurantInfo->getNamerestaurant(), $this->restaurantInfo->getStreet1().' '.$this->restaurantInfo->getZipcode().' '.ucFirst($this->restaurantInfo->getHalalmenuCity()));
 
     // set header and footer fonts
     $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
     $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
 
     // set default monospaced font
     $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
 
  //set margins
     $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
     $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
     $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
 
  //set auto page breaks
     $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
 
  //set image scale factor
     $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
 
  // ---------------------------------------------------------
 
  // set default font subsetting mode
     $pdf->setFontSubsetting(true);
 
  // Set font
  // dejavusans is a UTF-8 Unicode font, if you only need to
  // print standard ASCII chars, you can use core fonts like
  // helvetica or times to reduce file size.
     $pdf->SetFont('dejavusans', '', 12, '', true);
 
  // Add a page
  // This method has several options, check the source code documentation for more information.
     $pdf->AddPage();
 
  // Set some content to print
     $menu_pdf = "";


     $this->Departement = array(
            '75' => array('id' => 75, 'departement' => 'paris'),
            '77' => array('id' => 77, 'departement' => 'seine-et-marne'),
            '78' => array('id' => 78, 'departement' => 'yvelines'),
            '91' => array('id' => 91, 'departement' => 'essonne'),
            '92' => array('id' => 92, 'departement' => 'haut-de-seine'),
            '93' => array('id' => 93, 'departement' => 'saint-denis'),
            '94' => array('id' => 94, 'departement' => 'val-de-marne'),
            '95' => array('id' => 95, 'departement' => 'val-d-oise')
            );
  
                   //$menu_pdf = "";     
                   foreach($this->restaurantCategory as $Category)
                   {
                     $html = '<br/><h5 style="margin-bottom:0px;background-color:#FF9110;padding :0px;margin-left:10px;line-height: 10px;color:#FFFFFF;text-align: center;">'.$Category['category'].'</h5>';
                     $html.= '<table style="width: 100%">';
                         foreach($this->restaurantMeal as $key=>$Meal)
                         {
                            if(($Meal['HalalmenuCategory']['category']) == ($Category['category']))
                            {
                                $html.= '<tr><td style="width:88%;border-bottom: 1px solid #F0F0F0;line-height: 1.5em;padding-bottom: 3px;padding-top: 8px;text-align: left;color:#696767;font-size:8">'.$Meal.' '.$Meal['descriptionmeal'].'</td>';
                                $html.= '<td class="price" style="width:12%;text-aligh:right;border-bottom: 1px solid #F0F0F0;line-height: 1.5em;padding-bottom: 3px;padding-top: 8px;text-align: left;color:#696767;font-size:8">&nbsp;'.$Meal['price1'].'&nbsp;€ </td></tr>';
                            }
                         }
                      $html.= '</table>';
                      $menu_pdf =  $menu_pdf.$html;
                   }
          
          
  
       // Print text using writeHTMLCell()
     $pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $menu_pdf, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
 
  // ---------------------------------------------------------
 
  // Close and output PDF document
  // This method has several options, check the source code documentation for more information.
 
     $pdf->Output($this->restaurantInfo->getNamerestaurant().'.pdf', 'I');
 
     // Stop symfony process
     throw new sfStopException();
}

public function executeTest()
{
  $config = sfTCPDFPluginConfigHandler::loadConfig();
  sfTCPDFPluginConfigHandler::includeLangFile($this->getUser()->getCulture());
 
  $doc_title    = "test title";
  $doc_subject  = "test description";
  $doc_keywords = "test keywords";
  $htmlcontent  = "test";
 
  //create new PDF document (document units are set by default to millimeters)
  $pdf = new sfTCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true);
 
  // set document information
  $pdf->SetCreator(PDF_CREATOR);
  $pdf->SetAuthor(PDF_AUTHOR);
  $pdf->SetTitle($doc_title);
  $pdf->SetSubject($doc_subject);
  $pdf->SetKeywords($doc_keywords);
 
  $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
 
  //set margins
  $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
 
  //set auto page breaks
  $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
  $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
  $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); //set image scale factor
 
  $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
 
  //initialize document
  $pdf->AliasNbPages();
  $pdf->AddPage();
 
  // set barcode
  $pdf->SetBarcode(date("Y-m-d H:i:s", time()));
 
  // output some HTML code
  $pdf->writeHTML($htmlcontent , true, 0);
 
  // output two html columns
  $first_column_width = 80;
  $current_y_position = $pdf->getY();
  $pdf->writeHTMLCell($first_column_width, 0, 0, $current_y_position, "<b>hello</b>", 0, 0, 0);
  $pdf->writeHTMLCell(0, 0, $first_column_width, $current_y_position, "<i>world</i>", 0, 1, 0);
 
  // output some content
  $pdf->Cell(0,10,"TEST Bold-Italic Cell",1,1,'C');
 
  // output some UTF-8 test content
  $pdf->AddPage();
  $pdf->SetFont("FreeSerif", "", 12);
 
  $utf8text = file_get_contents(K_PATH_CACHE. "utf8test.txt", false); // get utf-8 text form file
  $pdf->SetFillColor(230, 240, 255, true);
  $pdf->Write(5,$utf8text, '', 1);
 
  // remove page header/footer
  $pdf->setPrintHeader(false);
  $pdf->setPrintFooter(false);
 
  // Two HTML columns test
  $pdf->AddPage();
  $right_column = "<b>right column</b> right column right column right column right column
  right column right column right column right column right column right column
  right column right column right column right column right column right column";
  $left_column = "<b>left column</b> left column left column left column left column left
  column left column left column left column left column left column left column
  left column left column left column left column left column left column left
  column";
  $first_column_width = 80;
  $second_column_width = 80;
  $column_space = 20;
  $current_y_position = $pdf->getY();
  $pdf->writeHTMLCell($first_column_width, 0, 0, 0, $left_column, 1, 0, 0);
  $pdf->Cell(0);
  $pdf->writeHTMLCell($second_column_width, 0, $first_column_width+$column_space, $current_y_position, $right_column, 0, 0, 0);
 
  // add page header/footer
  $pdf->setPrintHeader(true);
  $pdf->setPrintFooter(true);
 
  $pdf->AddPage();
 
  // Multicell test
  $pdf->MultiCell(40, 5, "A test multicell line 1\ntest multicell line 2\ntest multicell line 3", 1, 'J', 0, 0);
  $pdf->MultiCell(40, 5, "B test multicell line 1\ntest multicell line 2\ntest multicell line 3", 1, 'J', 0);
  $pdf->MultiCell(40, 5, "C test multicell line 1\ntest multicell line 2\ntest multicell line 3", 1, 'J', 0, 0);
  $pdf->MultiCell(40, 5, "D test multicell line 1\ntest multicell line 2\ntest multicell line 3", 1, 'J', 0, 2);
  $pdf->MultiCell(40, 5, "F test multicell line 1\ntest multicell line 2\ntest multicell line 3", 1, 'J', 0);
 
  // Close and output PDF document
  $pdf->Output();
 
  // Stop symfony process
  throw new sfStopExeption();
}


    public function executeSearch(sfWebRequest $request)
    {   
        $this->forwardUnless($query = $request->getParameter('query'), 'restaurant', 'index');

        $this->restaurants = Doctrine_Core::getTable('HalalmenuRestaurant') ->getForLuceneQuery($query);  
        
        $this->nb = count($this->restaurants);

    }
}

